﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace ASPNETVN.PORTAL.Modules.ANNOUNCEMENT.Components
{
    public partial class Item
    {
        public static IDataReader SelectReaderTop(Guid moduleID, int top)
        {
            const string spName = "[dbo].[p_ANNOUNCEMENT_Item_SelectTop]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@Top", SqlDbType.Int, top);
            return db.ExecuteReader(dbCommand);
        }
        //---------------------------------------------------------------------------------------------
        public static List<Item> SelectCollectionTop(Guid moduleID, int top)
        {
            List<Item> collection = new List<Item>();
            SqlDataReader reader = (SqlDataReader)SelectReaderTop(moduleID, top);
            while (reader.Read())
            {
                Item entity = new Item();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) entity.Description = reader.GetString(reader.GetOrdinal("Description"));
                if (!reader.IsDBNull(reader.GetOrdinal("ImageUrl"))) entity.ImageUrl = reader.GetString(reader.GetOrdinal("ImageUrl"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }
    }
}
